/*
* 文件：FPSShow.cs
* 作者：王阳
* 时间：2019-5-20
* 描述：显示帧率
*/

//#define ENABLED
#if ENABLED

using CSharpX;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace UnityX
{
    internal class FPSShow : MonoBehaviour
    {
        public Text text;
        public Vector2 anchorPoint = new Vector2(0, 1);
        public Color fontColor = new Color(0f, 1f, 0f);

        private int fps;
        private int frameCount1;
        private int frameCount2;
        private long ticks;

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
        private static void AfterSceneLoad()
        {
            GameObject canvaGameObject = new GameObject("_FPSShow_");
            canvaGameObject.hideFlags = HideFlags.HideInHierarchy;
            DontDestroyOnLoad(canvaGameObject);
            Canvas cv = canvaGameObject.AddComponent<Canvas>();
            cv.renderMode = RenderMode.ScreenSpaceOverlay;
            cv.sortingOrder = 32766;
            GameObject gameObject = new GameObject("_FPSShowText_");
            gameObject.transform.SetParent(canvaGameObject.transform, false);
            gameObject.AddComponent<FPSShow>();
        }

        // 仅在首次调用 Update 方法之前调用 Start
        private void Start()
        {
            if (text == null)
            {
                text = GetComponent<Text>();
                if (text == null)
                {
                    text = gameObject.AddComponent<Text>();
#if UNITY_2022_3_OR_NEWER
                    text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
#else
                    text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
#endif
                }
            }
            int fontSize = 16 + (int)Curve.L1(0, 12, 100, 500, Screen.dpi) + (int)Curve.L1(0, 24, 1000, 5000, Screen.width < Screen.height ? Screen.width : Screen.height);
            fontSize = fontSize & 0xFFFE;

            text.raycastTarget = false;
            text.alignment = TextAnchor.MiddleCenter;
            text.color = fontColor;
            text.fontSize = (int)fontSize;

            RectTransform rectTransform = gameObject.GetComponent<RectTransform>();
            rectTransform.pivot = anchorPoint;
            rectTransform.anchorMin = anchorPoint;
            rectTransform.anchorMax = anchorPoint;
            rectTransform.anchoredPosition = Vector2.zero;
            rectTransform.sizeDelta = new Vector2(fontSize * 6, fontSize * 4);

            frameCount1 = Time.frameCount;
            frameCount2 = Time.frameCount;
            ticks = DateTime.UtcNow.Ticks;
        }
        // 当对象已启用并处于活动状态时调用此函数
        private void OnEnable()
        {
            frameCount1 = Time.frameCount;
            frameCount2 = Time.frameCount;
            ticks = DateTime.UtcNow.Ticks;
        }
        // 当玩家暂停时发送给所有游戏对象
        private void OnApplicationPause(bool pause)
        {
            frameCount1 = Time.frameCount;
            frameCount2 = Time.frameCount;
            ticks = DateTime.UtcNow.Ticks;
        }
        // 如果 MonoBehaviour 已启用，则在每一帧都调用 Update
        private void Update()
        {
            if (DateTime.UtcNow.Ticks - ticks >= 500 * 10000)
            {
                ticks += 500 * 10000;
                fps = Time.frameCount - frameCount1;
                frameCount1 = frameCount2;
                frameCount2 = Time.frameCount;
                text.text = fps.ToString();
            }
        }
    }
}
#endif